/**
 * Created by xc on 15/12/22.
 */

var common = require('../../common');
var pool = require('../index');
var mysql = require('mysql');
var ItbBaseModel = require('./ItbBaseModel');


// Schema 结构
var ItbDocTypeRModel = function(obj){
    ItbBaseModel.call(this,obj);

  // 以下为表定义数据，需要的字段就填入
  //`doc_id` bigint(20) unsigned NOT NULL COMMENT '模版／文档ID',
  //`type_id` bigint(20) unsigned NOT NULL COMMENT '模版类型ID',
  //`create_time` datetime NOT NULL,
  //`update_time` datetime NOT NULL,
  //`update_by` varchar(50) NOT NULL DEFAULT '',
  //`del_flg` int(1) NOT NULL DEFAULT '0'


};
ItbDocTypeRModel.prototype = new ItbBaseModel();

// 取得表名
ItbDocTypeRModel.prototype.getTableName = function(){
    return 'itb_doc_type_r';
}
// 取得查询语句中where 后面匹配主键的条件（需要各派生类进行重载）
ItbDocTypeRModel.prototype.getWhereConditionOfKeyEqual = function(){
    return 'doc_id=' + this.doc_id + ' and type_id=' + this.type_id;
}

// 根据文档id取所有type名
ItbDocTypeRModel.prototype.getTypeNamesByDocId = function(callback){
    var THIS = this;
    var tableName = this.getTableName();

    pool.pool.getConnection(function(err, connection) {
        // Use the connection
        var sql = ' SELECT t2.type_id, t2.type_name ' +
            ' FROM itb_doc_type_r t1, itb_doc_type t2 ' +
            ' WHERE t1.doc_id =? AND t1.type_id = t2.type_id AND t1.del_flg = 0 AND t2.del_flg = 0 ';
        var sqlValue = [THIS.doc_id];
        var sql = mysql.format(sql, sqlValue);
        console.log(sql);
        connection.query(sql, function(error, result) {
            // And done with the connection.
            connection.release();
            console.log();

            callback(error, result);
        });
    });
}

// 根据文档id取所有type
ItbDocTypeRModel.prototype.getTypeInfoByDocId = function(callback){
    var THIS = this;
    var tableName = this.getTableName();

    pool.pool.getConnection(function(err, connection) {
        // Use the connection
        var sql = ' SELECT t2.* ' +
            ' FROM itb_doc_type_r t1, itb_doc_type t2 ' +
            ' WHERE t1.doc_id =? AND t1.type_id = t2.type_id AND t1.del_flg = 0 AND t2.del_flg = 0 ';
        var sqlValue = [THIS.doc_id];
        var sql = mysql.format(sql, sqlValue);
        console.log(sql);
        connection.query(sql, function(error, result) {
            // And done with the connection.
            connection.release();
            console.log();

            callback(error, result);
        });
    });
}

// 导出
module.exports = ItbDocTypeRModel;